Schema Introspection PRO
This section explains how to inspect and analyze database structures using SQLite’s schema introspection APIs.
Schema Introspection allows scripts to query database structure information at runtime, including tables, columns, primary keys, foreign keys, and indexes. These capabilities are commonly used for:
- Data migrations and version management
- Tooling scripts (such as database browsers or analyzers)
- Runtime schema validation
- Debugging and diagnostics
Getting the Schema Version
schemaVersion
Returns the current database schema version.
This value is typically used to:
- Determine whether a database migration is required
- Integrate with external schema versioning logic
Example:
Checking Table Existence
tableExists
Checks whether a specified table exists.
schemaNamedefaults to the main schema- Returns
trueif the table exists
Example:
Inspecting Columns
columnsIn
Returns structural information for all columns in the specified table.
ColumnInfo
Field descriptions:
name: column nametype: column typedefaultValueSQL: SQL expression for the default valueisNotNull: whether the column is NOT NULLprimaryKeyIndex: order of the column within the primary key (0 if not part of the primary key)
Example:
Inspecting Primary Keys
primaryKey
Returns primary key information for the specified table.
PrimaryKeyInfo
Field descriptions:
columns: names of the primary key columnsrowIDColumn: name of the associated rowid column, if applicableisRowID: whether SQLite’s implicit rowid is used as the primary key
Example:
Inspecting Foreign Keys
foreignKeys
Returns all foreign key definitions for the specified table.
ForeignKeyInfo
Field descriptions:
id: foreign key identifieroriginColumns: columns in the current tabledestinationTable: referenced tabledestinationColumns: referenced columnsmapping: one-to-one column mapping
Example:
Inspecting Indexes
indexes
Returns all indexes defined on the specified table.
IndexInfo
Field descriptions:
-
name: index name -
columns: columns included in the index -
isUnique: whether the index is unique -
origin: source of the index"createIndex": created explicitly viacreateIndex"primaryKeyConstraint": generated by a primary key constraint"uniqueConstraint": generated by a unique constraint
Example:
Checking Unique Key Combinations
isTableHasUniqueKeys
Checks whether the specified table has a unique constraint or unique index that exactly matches the given column combination.
Example:
This method is commonly used to:
- Determine whether a unique index needs to be created
- Avoid redefining constraints during schema initialization or migration
Usage Recommendations
- Inspect existing schema state before applying structural changes
- Prefer introspection over assumptions in migration logic
- Combine introspection APIs with tooling scripts for analysis or visualization
- Avoid calling schema inspection APIs in high-frequency execution paths
Summary
Schema Introspection provides runtime visibility into database structure, enabling scripts to safely and reliably understand the current database state.
It is commonly used in combination with:
- Schema Management for defining and modifying structures
- Transactions to ensure atomic schema changes
- Executing SQL & Queries for data operations based on known schemas
